package cn.edu.scau.cmi.liangzijian.composite.safe;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SafeTeam implements SafeOrganization {
	private List<SafeOrganization> memberList = new ArrayList<SafeOrganization>();
	//private Set<SafeOrganization> membsetSet = new HashSet<SafeOrganization>();
	private String name;
	
	private SafeOrganization parent;
	
	public SafeTeam(String name) {
		this.name = name;
	}
	
	public boolean add(SafeOrganization p) {
		String name = p.getName();
		
		for(SafeOrganization so : memberList) {
			if(name.equals(so.getName())) {
				if((p instanceof SafePerson && so instanceof SafePerson)||
						p instanceof SafeTeam && so instanceof SafeTeam) {
					return false;
				} 
			}
		}
		p.setParent(this);
		memberList.add(p);
		return true;
	}
		
	public boolean remove(String name) {
		for(SafeOrganization so : memberList) {
			if(name.equals(so.getName())) {
				memberList.remove(so);
				return true;
			}
		}
		
		return false;
	}
	
	public boolean remove(SafeOrganization p) {
		return memberList.remove(p);
	}
	
	public SafeOrganization getOrganization(String name) {
		for(SafeOrganization so : memberList) {
			if(name.equals(so.getName())) {
				return so;
			}
		}
		
		return null;
	}
	
	public List<SafeOrganization> getMemberList() {
		return memberList;
	}
	
	@Override
	public void say() {
		for(SafeOrganization person : memberList) {
			person.say();
		}
	}

	@Override
	public String getName() {
		return name;
	}
	
	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void setParent(SafeOrganization parent) {
		this.parent = parent;
		
	}

	@Override
	public SafeOrganization getParent() {
		return parent;
	}
}
